<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

class SubsetPage {

	private $alerter;
	
	public function __construct() {
		$this->alerter = new Alerter();
	}

	/**
	 * adds extra access checks, dependent on page state
	 */
	function page_inc_check_access($page) {
		check_access("READ");
	}

	function page_inc_execute_action($page, $action) {
		$THISDIR = $page->directory_tree[$page->dir_depth-1];

		switch($action) {
			case "markread":
				if (!$_SESSION['user_id'])
					errorpage(6);
				$from = array("ITEM_IN", "DUMMY");
				$where = "ITEM_IN.sub_id = {$page->id}";
				$sql = db_query_select(array(array(), "ITEM_IN.item_id"), $from, $where);
				$sqlresult = db_do_query($page, $sql);
				$values = array();
				while ($r = db_fetch_array($sqlresult))
					array_push($values, array($r['item_id'], $_SESSION['user_id']));
				$sql = db_query_insert_multiple(array(item_id, user_id), $values, "HAS_READ", 1);
				db_do_query($page, $sql);
				// TODO crossposted dirs should be updated as well
				$n = db_affected_rows();
				$page->menu[$THISDIR['dir_id']]['new_items'] -= $n;
				$page->menu[$THISDIR['dir_id']]['read_items'] += $n;
				$this->alerter->add_alert("alles gelezen hier...");
				break;
			default:
				$this->alerter->add_alert("unknown action: $action");
				break;
		}
	}

	/**
	 * execute page logic
	 */
	function page_inc_execute($page) {
		global $CONFIG;
		$CONTENT = array();
		$THISDIR = $page->directory_tree[$page->dir_depth-1];
		
		$CONTENT['items'] = array();

		$where = "ITEM.item_type_id = ITEM_TYPE.item_type_id AND 
				  ITEM.item_id = ITEM_IN.item_id AND
				  ITEM_IN.sub_id = {$page->id} AND
				  ITEM.author = M1.user_id AND
				  ITEM.editor = M2.user_id AND 
				  NOT ITEM_IN.deleted AND 
				  NOT ITEM.deleted";

		$order = "sticky DESC, {$THISDIR['sorting_sql']}";

		$sql = db_query_select(array(array("*"), array("*"), array(), array(), array(), "M1.nick as nick_author, M2.nick as nick_editor"), array("ITEM", "ITEM_TYPE", "ITEM_IN", "MEMBER AS M1", "MEMBER AS M2", "DUMMY"), $where, $order);
		$items = db_do_query($page, $sql);

		$read = array();
		if ($_SESSION['user_id']) {
			$readings = db_do_query($page, db_query_select(array(array(), "HAS_READ.item_id"), array("(((HAS_READ INNER JOIN ITEM ON HAS_READ.item_id = ITEM.item_id) INNER JOIN ITEM_IN ON HAS_READ.item_id = ITEM_IN.item_id) INNER JOIN SUB_IN ON ITEM_IN.sub_id = SUB_IN.sub_id)" . ($THISDIR['show_subsets'] ? " INNER JOIN SUBSET ON SUB_IN.sub_id = SUBSET.sub_id" : "")), "SUB_IN.dir_id = {$THISDIR['dir_id']} AND user_id = {$_SESSION['user_id']}", $order, $limit));
			while ($item = db_fetch_array($readings))
				$read[$item['item_id']] = 1;
		}

		while ($item = db_fetch_array($items)) {
			$item['unread'] = $_SESSION['user_id'] ? !$read[$item['item_id']] : 0;
			$item['new'] = (($item['edit'] > $_SESSION['member']['last_visit']) and $item['unread']) ? 1 : 0;
			array_unshift($CONTENT['items'], $item);
		}

		if ($_SESSION['user_id']) {
			
			// mark all items read
			add_action('markread', 'url-action', 'markread');
		
			// adding
			if (has_access("ADD"))
				add_action('add_item', 'url', ".add_item?sub_id={$page->id}");
				
			// edit subset settings
			if (has_access("MODERATE") or has_access("ADMIN"))
				add_action('edit_sub', 'url', "{$page->id}.edit_sub");
		}
		
		$sql = db_query_update(array('hits'), array('hits + 1'), "DIRECTORY", "dir_id = {$THISDIR['dir_id']}");
		db_do_query($page, $sql);

		$sql = db_query_select(array(array('dir_id'), array(), "BODY.body, SUB_IN.sub_title, SUBSET.sub_id"), array("SUB_IN", "SUBSET LEFT JOIN BODY ON SUBSET.body_id = BODY.body_id", "DUMMY"), "SUB_IN.dir_id = {$THISDIR['dir_id']} AND SUB_IN.sub_id = SUBSET.sub_id AND SUBSET.sub_id = {$page->id}");
		$sqlresult = db_do_query($page, $sql);
		$CONTENT['subset_indirs'] = array();
		while ($s = db_fetch_array($sqlresult)) {
			$CONTENT['subset'] = $s;
			$CONTENT['subset_indirs'][] = $s['dir_id'];
		}
		
		$CONTENT['title'] = $CONTENT['subset']['sub_title'];
			
		$page->content = $CONTENT;
	}

	/**
	 * dress up page content using skin stuff
	 */
	function page_inc_dress($page) {
		$subsetPage = new Page();
		$subsetPage->content['body'] = $page->content['subset']['body']; //TODO: this is a silly workaround. revise.
		print_body($subsetPage); 
		print_itemlist($page, $page->content['items'], $page->dir_path, 0);
	}
}

?>